<template>
  <div class="layout">
    <div class="login">
      <div class="logo">
        <div class="lo"><img src="@/assets/logo.jpg" /></div>
      </div>
      <div class="user">
        <el-form :model="form" ref="loginForm">
          <div class="tip" :class="{ 'show': isShow }">
            <img src="../../assets/images/tip.png" style="height: 18px; width: 18px; vertical-align: middle;">
            请联系管理员重置密码
          </div>
          <el-form-item prop="username" class="margin-18">
            <div class="input-box">
              <el-image style="width: 24px; height: 24px" src="../src/assets/images/联系人.svg" />
              <el-input v-model="form.username" placeholder="请输入账号" :maxlength="150" @input="checkName" />
            </div>
            <div class="inputTip" v-if="isName">请输入用户名</div>
          </el-form-item>
          <el-form-item prop="password" class="margin-18">
            <div class="input-box">
              <el-image style="width: 24px; height: 24px" src="../src/assets/images/密码.svg" />
              <el-input v-model="form.password" placeholder="请输入密码" type="password" :maxlength="150"
                @input="checkPassword" />
            </div>
            <div class="inputTip" v-if="isPassword">请输入密码</div>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="login">登录</el-button>
          </el-form-item>
          <div style="width: 100%; ">
            <div class="forget" @click="forgetTip">
              忘记密码
            </div>
          </div>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { useRouter } from 'vue-router'
import { adminLogin } from '@/api/login'
import { ElMessage } from 'element-plus';
import { useUserStore } from '@/stores';


// 使用 Vue Router 实例
const router = useRouter()

// 定义表单的 reactive 对象

const form = reactive({
  username: '',
  password: ''
})
// 通过校验表单来展示提示信息
let isName = ref(false)
let isPassword = ref(false)
function checkName() {
  let name = form.username.trim()
  if (name === '') {
    console.log(name === '')
    isName.value = true
  } else {
    isName.value = false
  }
}
function checkPassword() {
  let password = form.password.trim()
  if (password === '') {
    isPassword.value = true
  } else {
    isPassword.value = false
  }
}
// 表单提交函数
const login = async () => {
  checkName();
  checkPassword();

  let username = form.username.trim();
  let password = form.password.trim();

  if (username && password) {
    try {
      const response = await adminLogin({ username, password });

      if (response.data && response.data.code === 1) {
        // 假设 response.data 包含你需要保存的用户数据
        const userData = response.data.data;

        // 使用 Pinia store 保存用户数据
        const userStore = useUserStore();
        userStore.setUserInfo(userData);

        // 登录成功，显示提示框
        ElMessage.success('登录成功！');

        // 登录成功后，跳转到菜单页面
        router.push('/menu');
      } else {
        // 登录失败，显示提示框
        ElMessage.error('登录失败，请检查用户名和密码！');
      }
    } catch (error) {
      console.error('登录失败:', error);
      // 登录失败，显示提示框
      ElMessage.error('登录失败，请检查用户名和密码！');
    }
  } else {
    // 用户名或密码为空时，显示提示框
    ElMessage.warning('请填写用户名和密码');
  }

};

// 忘记密码点击
let isShow = ref(false)
function forgetTip() {
  isShow.value = true
  let tid = setTimeout(() => {
    isShow.value = false
  }, 3000)

}
</script>

<style scoped lang="scss">
html,
body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
}

.layout {
  width: 100vw;
  height: 100vh;
  background-color: #f5f5f5;
  display: flex;
  justify-content: center;
  /* 水平居中 */
  align-items: center;
  /* 垂直居中 */
  position: absolute;
  /* 使内容相对于视口定位 */
  top: 50%;
  /* 距离顶部50% */
  left: 50%;
  /* 距离左侧50% */
  transform: translate(-50%, -50%);
  /* 使用transform将内容偏移，保证真正的居中 */

}

.login {
  width: 990px;
  height: 510px;
  display: flex;
  background-color: #fff;
  box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.2);
  border-radius: 10px;

  .logo {
    flex: 3;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.2);
    background-color: #fff;
    border-radius: 10px 0 0 10px;

    .lo {
      width: 70px;
      height: 70px;
      color: #fff;
      background: linear-gradient(to bottom, #011b43, #056b96);
      border-radius: 10px;
      font-size: 10px;
      display: flex;
      justify-content: center;
      align-items: center;

      img {
        width: 150px;
      }
    }

    .infor {
      margin-top: 35px;
      font-size: 24px;
      color: #034871;

    }
  }

  .user {
    flex: 2;
    display: flex;
    justify-content: center;
    align-items: center;
    box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.2);
    border-radius: 0 10px 10px 0;



    .el-input__wrapper {
      padding: 1px 2px !important;
    }

    .tip {
      display: flex;
      justify-content: center;
      align-items: center;
      font-size: 14px;
      color: #666;
      margin-bottom: -10px;
      opacity: 0;
      transition: opacity 1s ease;
    }

    .show {
      opacity: 1;
    }

    .el-form-item {
      height: 38px;
      width: 300px;
      margin-top: 30px;
    }

    .input-box {
      width: 300px;
      height: 45px;
      display: flex;
      /* 使图片和输入框水平排列 */
      align-items: center;
      /* 垂直居中 */
      border: 1px solid #ccc;
      /* 添加灰色边框 */
      padding: 5px;

      /* 内边距，确保内容不贴着边框 */



    }

    .el-input {
      margin-left: 10px;
      /* 给图片和输入框之间添加间距 */
      height: 40px;
      font-size: 14px;
    }

    .inputTip {
      height: 15px;
      line-height: 15px !important;
      color: red !important;
      font-size: 12px !important;

    }

    :deep(.el-input__wrapper) {
      align-items: center;
      box-shadow: none;
    }

    .el-button {

      width: 100%;
      background: #03a27b;
      border: none;
      color: #fff;
      height: 35px;
      line-height: 35px;
    }

    .forget {
      margin-left: 80%;
      width: fit-content;
      cursor: pointer;
      margin-top: 60px;
      text-align: right;
      font-size: 14px;
      color: #666;
    }
  }
}
</style>